#!/bin/bash

export PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
clear
printf "
#######################################################################
#                               LNMP                                  #
#######################################################################
"

#init install dir
sed -i "s@^oneinstack_dir.*@oneinstack_dir=`pwd`@" ./options.conf

. ./apps.conf
. ./options.conf
. ./include/color.sh
. ./include/check_os.sh
. ./include/download.sh

# check if user is root
[ $(id -u) != "0" ] && { echo "${CFAILURE}Error: You must be root to run this script${CEND}"; exit 1; }

mkdir -p $wwwroot_dir/default $wwwlogs_dir
[ -d /data ] && chmod 755 /data

while :; do echo
  read -p "Do you want to install web server? [y/n]:" Web_yn
  if [[ ! $Web_yn =~ ^[y,n]$ ]]; then
    echo "${CWARNING}input error! Please input only 'y' or 'n'${CEND}"          
  else
    if [ "$Web_yn" == 'y' ]; then
		#Nginx/Openresty
		while :; do
			echo 'Please select web server:'
			echo -e "\t${CMSG}1${CEND}. Install Nginx"
			echo -e "\t${CMSG}2${CEND}. Install Openresty"
			echo -e "\t${CMSG}3${CEND}. Do not install"
			read -p "Please input a number:(Default 1 press Enter) " Nginx_version
			[ -z "$Nginx_version" ] && Nginx_version=1
			if [[ ! $Nginx_version =~ ^[1-3]$ ]];then
				echo "${CWARNING}input error! Please only input number 1,2,3${CEND}"
			else
				[ "$Nginx_version" != '3' -a -e "$nginx_install_dir/sbin/nginx" ] && { echo "${CWARNING}Nginx already installed! ${CEND}"; Nginx_version=Other; }
				[ "$Nginx_version" != '3' -a -e "$openresty_install_dir/sbin/nginx" ] && { echo "${CWARNING}Openresty already installed! ${CEND}"; Nginx_version=Other; }
				break
			fi
		done
	fi
	break
  fi
done

#choice database
while :; do echo
	read -p "Do you want to install database? [y/n]:" Db_yn
	if [[ ! $Db_yn =~ ^[y,n]$ ]];then
		echo "${CWARNING}input error! Please input only 'y' or 'n'${CEND}"
	else
		if [ $Db_yn == 'y' ]; then
			[ -d "$db_install_dir/support-files" ] && { echo "${CWARNING}Database already installed! ${CEND}"; DB_yn=Other; break; }
			while :; do echo 
				echo 'Please select a version of database:'
				echo -e "\t${CMSG}1${CEND}. Install MySQL-5.7"
				echo -e "\t${CMSG}2${CEND}. Install MySQL-5.6"
				read -p "Please input a number:(Default 2 press Enter) " DB_version
				[ -z "$DB_version" ] && DB_version=2
				if [[ ! $DB_version =~ ^[1-2]$ ]]; then
					echo "${CWARNING}input error! Please only input number 1,2${CEND}"
				else 
					while :; do
						read -p "Please input the root password of database:" dbrootpwd
						[ -n "`echo $dbrootpwd | grep '[+|&]'`" ] && { echo "${CWARNING}input error,not contain a plus sign (+) and & ${CEND}"; continue; }
						(( ${#dbrootpwd} >= 5 )) && sed -i "s+^dbrootpwd.*+dbrootpwd='$dbrootpwd'+" ./options.conf && break || echo "${CWARNING}database root password least 5 characters! ${CEND}"
					done
					break
				fi
			done
		fi
		break
	fi
done

#chek PHP
while :; do echo
	read -p "Do you want install PHP? [y/n]:" PHP_yn
	if [[ ! $PHP_yn =~ ^[y,n]$ ]]; then
		echo "${CWARNING}input error! Please input only 'y' or 'n'${CEND}"
	else
		if [ $PHP_yn == 'y' ]; then
			while :; do echo
				echo "Please select a version of PHP:"
				echo -e "\t${CMSG}1${CEND}. Install php-5.6"
				echo -e "\t${CMSG}2${CEND}. Install php-7"
				read -p "Please input a number:(Default 1 press Enter) " PHP_version
				[ -z "$PHP_version" ] && PHP_version=1
				if [[ ! $PHP_version =~ ^[1-2]$ ]]; then
					echo "${CWARNING}input error! Please only input number 1,2${CEND}"
				else
					while :; do echo
						read -p "Do you want to install cache of PHP? [y/n]:" PHP_cache_yn
						if [[ ! $PHP_cache_yn =~ ^[y,n]$ ]]; then
							echo "${CWARNING}input error! Please input only 'y' or 'n'${CEND}"
						else
							if [ $PHP_cache_yn == 'y' ]; then
								if [ $PHP_version == 1 ]; then
									while :; do
										echo "Please select opcache of PHP:"
										echo -e "\t${CMSG}1${CEND}. Install Zend OPcache"
                                        echo -e "\t${CMSG}2${CEND}. Install XCache"
                                        read -p "Please input a number:(Default 1 press Enter) " PHP_cache
                                        [ -z "$PHP_cache" ] && PHP_cache=1
										if [[ ! $PHP_cache =~ ^[1-2]$ ]];then
                                            echo "${CWARNING}input error! Please only input number 1,2,3${CEND}"
                                        fi
										break
									done
								fi

								if [ $PHP_version == 2 ]; then
									while :; do
                                        echo 'Please select a opcode cache of the PHP:'
                                        echo -e "\t${CMSG}1${CEND}. Install Zend OPcache"
                                        read -p "Please input a number:(Default 1 press Enter) " PHP_cache
                                        [ -z "$PHP_cache" ] && PHP_cache=1
                                        if [ $PHP_cache != 1 ];then
                                            echo "${CWARNING}input error! Please only input number 1${CEND}"
                                        fi
                                        break
                                    done
								fi
							fi
							break
						fi
					done

					
					break
						
				fi
			done
		fi
		break
	fi
done

#check Redis
while :; do echo
    read -p "Do you want to install redis? [y/n]: " redis_yn
    if [[ ! $redis_yn =~ ^[y,n]$ ]];then
        echo "${CWARNING}input error! Please only input 'y' or 'n'${CEND}"
    else
    	break
    fi
	
done

#check Git
while :; do echo
	read -p "Do you want to install Git? [y/n]: " git_yn
	if [[ ! $git_yn =~ ^[y,n]$ ]]; then
		echo "${CWARNING}input error! Please only input 'y' or 'n'${CEND}"
	else
		break
	fi
done

#check GitLab
while :; do echo
	read -p "Do you want to install GitLab? [y/n]: " gitLab_yn
	if [[ ! $gitLab_yn =~ ^[y,n]$ ]]; then
		echo "${CWARNING}input error! Please only input 'y' or 'n'${CEND}"
	else
		if [ "$gitLab_yn" == 'y' ];then
			gitLab_dependent_flag=
			#check ruby
			which ruby > /dev/null 2>&1
			if [ $? != 0 ]; then
				echo "${CWARNING}not found ruby!${CEND}"
				gitLab_dependent_flag="ruby "
			fi

			which git > /dev/null 2>&1
			if [ $? != 0 ]; then
				echo "${CWARNING}not found git!${CEND}"
				gitLab_dependent_flag="${gitLab_dependent_flag}git"
			fi

			if [ -n gitLab_dependent_flag ]; then
				while :; do echo
					read -p "${gitLab_dependent_flag} not install, do you want to install? [y/n] :" gitlab_dependency_yn
					if [[ ! $gitLab_yn =~ ^[y,n]$ ]]; then
						echo "${CWARNING}input error! Please only input 'y' or 'n'${CEND}"
					fi
					break
				done
			fi
		fi
		break
	fi
done

#init
. ./include/memory.sh
if [ "$OS" == "CentOS" ]; then
	if [ ! -f $oneinstack_dir/system_init.log ]; then
		. include/init_CentOS.sh > $oneinstack_dir/system_init.log 2>&1 | tee $oneinstack_dir/install.log
	fi
	[ -n "`gcc --version | head -n1 | grep '4\.1\.'`" ] && export CC="gcc44" CXX="g++44"
fi

#database
if [ "$DB_version" == '1' ]; then
	. include/mysql-5.7.sh
	Install_MySQL-5-7 2>&1 | tee -a $oneinstack_dir/install.log
elif [ "$DB_version" == '2' ]; then
	. include/mysql-5.6.sh
	Install_MySQL-5-6 2>&1 | tee -a $oneinstack_dir/install.log
fi

#php
if [ "$PHP_version" == '1' ]; then
	. include/php-5.6.sh
	Install_PHP-5-6 2>&1 | tee -a $oneinstack_dir/install.log
elif [ "$PHP_version" == '2' ]; then
	. include/php-7.sh
	Install_PHP-7 2>&1 | tee -a $oneinstack_dir/install.log
fi

# Web server
if [ "$Nginx_version" == '1' ];then
    . include/nginx.sh
    Install_Nginx 2>&1 | tee -a $oneinstack_dir/install.log
elif [ "$Nginx_version" == '2' ];then
    . include/tengine.sh
    Install_Tengine 2>&1 | tee -a $oneinstack_dir/install.log
fi


# redis
if [ "$redis_yn" == 'y' ];then
    . include/redis.sh
    [ ! -d "$redis_install_dir" ] && Install_redis-server 2>&1 | tee -a $oneinstack_dir/install.log
    # [ -e "$php_install_dir/bin/phpize" ] && [ ! -e "`$php_install_dir/bin/php-config --extension-dir`/redis.so" ] && Install_php-redis 2>&1 | tee -a $oneinstack_dir/install.log
fi

#git
if [ "$git_yn" == 'y' ]; then
	. include/git.sh
	Install_Git 2>&1 | tee -a $oneinstack_dir/install.log
fi

#gitLab
if [ "$gitLab_yn" == 'y' ]; then
	if [ "$gitlab_dependency_yn" == 'y' ]; then
		gitLab_dependencies=($gitLab_dependent_flag)
		dependency_count=${#gitLab_dependencies[@]}
		if [ $dependency_count > 0 ]; then
			for dependency in ${gitLab_dependencies[@]}
			do
				. include/$dependency.sh
				Install_${dependency^} 2>&1 | tee -a $oneinstack_dir/install.log
			done
		fi
	fi
	. include/gitLab.sh
	Install_GitLab 2>&1 | tee -a $oneinstack_dir/install.log
fi